package com.clayecloud.smartcommunity.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.clayecloud.lib_common.room.ClientDataBase;
import com.clayecloud.lib_common.room.entity.ClientBase;
import com.clayecloud.smartcommunity.R;

import com.clayecloud.smartcommunity.databinding.ActivityMainBinding;
import com.gyf.immersionbar.ImmersionBar;

import java.util.Timer;
import java.util.TimerTask;

import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;

@Route(path = "/app/index")
public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding activityMainBinding;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        //沉浸式状态栏
        ImmersionBar.with(this).init();
        //定时跳转
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
                ARouter.getInstance()
                        .build("/main/index")
                        .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)
                        .withTransition(R.anim.window_ios_in, R.anim.window_fake)
                        .navigation(activityMainBinding.getRoot().getContext());
            }
        }, 3000);

        initDataBase();
        //引导激活后，走引导（真假引导待定）

    }

    /**
     * 初始化数据库数据
     */
    private void initDataBase() {
        //初始化登录id
        ClientDataBase.getClientDataBaseInstance().getClientBaseService().getClientBaseById(1).observe(this, new Observer<ClientBase>() {
            @Override
            public void onChanged(ClientBase clientBase) {
                if(clientBase == null){
                    ClientDataBase.getClientDataBaseInstance().getClientBaseService().insertClientBase(new ClientBase(1,0)).subscribe(new SingleObserver<Long>() {
                        private Disposable disposable;
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {
                            disposable = d;
                        }

                        @Override
                        public void onSuccess(@NonNull Long aLong) {
                            if(aLong > 0){
                                Log.i("HomeFragment =>getClientBaseById => onSuccess ","初始化成功");
                            }else{
                                Log.i("HomeFragment =>getClientBaseById => onSuccess ","初始化失败");
                            }
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            Log.e("HomeFragment =>getClientBaseById => onError ",e.getMessage());
                            disposable.dispose();
                        }
                    });
                    //未来引导的插入点
                }
            }
        });

    }

}